package com.mfl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mfl.common.PageResult;
import com.mfl.constant.MessageConstant;
import com.mfl.context.BaseContext;
import com.mfl.exception.TestCategoryAlreadyExistsException;
import com.mfl.mapper.*;
import com.mfl.pojo.dto.TestCategoryDTO;
import com.mfl.pojo.dto.TestCategoryPageQueryDTO;
import com.mfl.pojo.entity.TestCategory;
import com.mfl.pojo.entity.TestExamTopic;
import com.mfl.pojo.vo.TestCategoryPageQueryVO;
import com.mfl.pojo.vo.TestCategoryVO;
import com.mfl.service.TestCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class TestCategoryServiceImpl implements TestCategoryService {

    @Autowired
    private TestCategoryMapper testCategoryMapper;
    @Autowired
    private TestExamMapper testExamMapper;
    @Autowired
    private TestExamTopicMapper testExamTopicMapper;
    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private ExamDetailMapper examDetailMapper;

    /**
     * 新增测试分类
     *
     * @param testCategoryDTO
     */
    @Override
    public void insert(TestCategoryDTO testCategoryDTO) {
        // 查询分类是否存在
        TestCategory testCategory = testCategoryMapper.getByTestName(testCategoryDTO.getTestName());
        if (testCategory != null) {
            throw new TestCategoryAlreadyExistsException(MessageConstant.TEST_CATEGORY_ALREADY_EXISTS);
        }
        TestCategory build = TestCategory.builder()
                .testName(testCategoryDTO.getTestName())
                .testTime(testCategoryDTO.getTestTime())
                .status(testCategoryDTO.getStatus())
                .createUser(BaseContext.getCurrentId())
                .createTime(LocalDateTime.now())
                .build();
        testCategoryMapper.insert(build);
    }

    /**
     * 分页查询测试分类
     *
     * @param testCategoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(TestCategoryPageQueryDTO testCategoryPageQueryDTO) {
        PageHelper.startPage(testCategoryPageQueryDTO.getPage(), testCategoryPageQueryDTO.getPageSize());
        Page<TestCategoryPageQueryVO> page = testCategoryMapper.pageQuery(testCategoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 修改测试分类状态
     *
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(int status, Integer id) {
        testCategoryMapper.updateStatus(status, id);
    }

    /**
     * 批量删除测试分类
     *
     * @param ids
     */
    @Transactional
    @Override
    public void batchDelete(List<Integer> ids) {
        testCategoryMapper.deleteByIds(ids);
        // 根据分类的id获取到对应的所有的试卷id
        List<Integer> testExamIds = testExamMapper.getIdByTestCategoryId(ids);
        // 删除测试分类对应的测试卷
        testExamMapper.deleteByTestCategoryIds(ids);
        // 并且删除测试卷对应的题目
        testExamTopicMapper.deleteByTestExamIds(testExamIds);
        // 根据分类id获取对应的所有考试的id
        List<Integer> examIds = examMapper.getIdByTestCategoryId(ids);
        if (examIds.size() > 0) {
            // 删除对应的考试记录
            examMapper.deleteByIds(examIds);
            // 删除对应的考试详情
            examDetailMapper.deleteByExamIds(examIds);
        }
    }

    /**
     * 根据id查询测试分类
     *
     * @param id
     * @return
     */
    @Override
    public TestCategoryVO getTestCategoryById(Integer id) {
        TestCategory testCategory = testCategoryMapper.getByTestCategoryId(id);
        return TestCategoryVO.builder()
                .id(testCategory.getId())
                .testName(testCategory.getTestName())
                .testTime(testCategory.getTestTime())
                .status(testCategory.getStatus())
                .build();
    }

    /**
     * 修改测试分类
     *
     * @param testCategoryDTO
     */
    @Override
    public void update(TestCategoryDTO testCategoryDTO) {
        TestCategory testCategory = TestCategory.builder()
                .id(testCategoryDTO.getId())
                .testName(testCategoryDTO.getTestName())
                .testTime(testCategoryDTO.getTestTime())
                .status(testCategoryDTO.getStatus())
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        System.out.println(testCategory);
        testCategoryMapper.update(testCategory);
    }

    /**
     * 查询所有测试分类
     *
     * @return
     */
    @Override
    public List<TestCategoryVO> getAll() {
        return testCategoryMapper.getAll();
    }
}
